package com.ywl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ywl.dto.OrdersDto;
import com.ywl.entity.HouseDetail;
import com.ywl.entity.Orders;
import com.ywl.mapper.OrderMapper;
import com.ywl.service.HouseDetailService;
import com.ywl.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywl.utils.MyStringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywl
 * @since 2022-08-26
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
    @Autowired
    HouseDetailService houseDetailService;

    @Override
    public List<OrdersDto> getByPhone(String phone) {
        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        qw.eq(StringUtils.isNotEmpty(phone), Orders::getPhone,phone);
        List<Orders> orderList = super.list(qw);

        List<OrdersDto> orderDtoList = orderList
                .parallelStream()
                .map(order -> {
                    log.info("{}"+Thread.currentThread().getId());
                    OrdersDto orderDto = new OrdersDto();
                    BeanUtils.copyProperties(order,orderDto);
                    Long detailId = order.getDetailId();
                    HouseDetail houseDetail = houseDetailService.getById(detailId);
                    BeanUtils.copyProperties(houseDetail,orderDto,"id");
                    //进行字符串的处理
                    /*
                    String imgs = houseDetail.getImgsString();
                    String firstImg = MyStringUtils.getFirstImg(imgs,",");
                    orderDto.setImg(firstImg);
                    log.info("firstImg={}",firstImg);
                    */
                    orderDto.setImg(houseDetail.getImg());
                    return orderDto;
                }).collect(Collectors.toList());
        return orderDtoList;
    }
}
